package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsCouponServiceApi;
import com.itheima.hmshopping.dubbo.mapper.SmsCouponMapper;
import com.itheima.hmshopping.entity.SmsCoupon;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponServiceApiImpi implements SmsCouponServiceApi {

    @Autowired
    private SmsCouponMapper smsCouponMapper;
    //根据优惠券名称和类型分页获取优惠券列表
    @Override
    public Page list(String name, Integer pageNum, Integer pageSize, String type) {
        Page<SmsCoupon> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsCoupon> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //模糊查询名字
        lambdaQueryWrapper.like(StrUtil.isNotBlank(name),SmsCoupon::getName,name);
        //如果优惠卷存在,筛选
        if (ObjectUtil.isNotEmpty(type)){
            if ("全场赠券".equals(type)){
                lambdaQueryWrapper.eq(SmsCoupon::getType,0);
            }else if ("会员赠券".equals(type)){
                lambdaQueryWrapper.eq(SmsCoupon::getType,1);
            }else if ("购物赠券".equals(type)){
                lambdaQueryWrapper.eq(SmsCoupon::getType,2);
            }else {
                lambdaQueryWrapper.eq(SmsCoupon::getType,3);
            }
        }
        Page<SmsCoupon> smsCouponPage = smsCouponMapper.selectPage(page, lambdaQueryWrapper);
        return smsCouponPage;
    }

    //添加优惠券
    @Override
    public void insert(SmsCoupon smsCoupon) {
        smsCouponMapper.insert(smsCoupon);
    }

    //通过id删除
    @Override
    public void delect(Integer id) {
        Long aLong = Convert.toLong(id);
        smsCouponMapper.deleteById(aLong);
    }

    //id查询
    @Override
    public SmsCoupon selectById(Integer id) {
        Long aLong = Convert.toLong(id);
        SmsCoupon smsCoupon = smsCouponMapper.selectById(aLong);
        return smsCoupon;
    }

    //更新优惠劵
    @Override
    public void update(SmsCoupon smsCoupon) {
       smsCouponMapper.updateById(smsCoupon);
    }
}
